package com.hsjk.szwj.module.train.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hsjk.szwj.module.train.api.dto.OrderDTO;
import com.hsjk.szwj.module.train.api.dto.OrderUpdateDTO;
import com.hsjk.szwj.module.train.convert.OrderConvert;
import com.hsjk.szwj.module.train.dal.dataobject.order.OrderDO;
import com.hsjk.szwj.module.train.dal.mysql.order.IOrderMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author chenjiawei
 * @desc
 * @Date 2023/08/28 23:02
 **/
@Service
public class OrderApiImpl implements IOrderApi {

    @Resource
    private IOrderMapper orderMapper;

    @Override
    public OrderDTO getOrderInfoById(Long orderId) {
        OrderDO orderDO = orderMapper.selectById(orderId);
        return OrderConvert.INSTANCE.convertOrderDTO(orderDO);
    }

    @Override
    public OrderDTO getOrderInfoByOrderNo(String orderNo) {
        OrderDO orderDO = orderMapper.selectOne(new LambdaQueryWrapper<OrderDO>()
                .eq(OrderDO::getOrderNo, orderNo));
        return OrderConvert.INSTANCE.convertOrderDTO(orderDO);
    }

    @Override
    public Boolean updateOrderInfo(OrderUpdateDTO orderUpdateDTO) {
        OrderDO orderDO = new OrderDO();
        orderDO.setStatus(orderUpdateDTO.getStatus());
        orderDO.setPayTime(orderUpdateDTO.getPayTime());
        orderDO.setPayOrderId(orderUpdateDTO.getPayOrderId());
        int resultCount = orderMapper.update(orderDO, new LambdaQueryWrapper<OrderDO>()
                .eq(OrderDO::getOrderNo, orderUpdateDTO.getOrderNo()));
        return resultCount > 0;
    }
}
